<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Mask generation function</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Mask_generation_function"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Mask_generation_function rootpage-Mask_generation_function skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Mask generation function</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>
A <b>mask generation function</b> (<b>MGF</b>) is a cryptographic primitive similar to a <a href="Cryptographic_hash_function" title="Cryptographic hash function">cryptographic hash function</a> except that while a hash function's output has a fixed size, a MGF supports output of a variable length. In this respect, a MGF can be viewed as a <a href="Extendable-output_function" title="Extendable-output function">extendable-output function</a> (XOF): it can accept input of any length and process it to produce output of any length. Mask generation functions are completely deterministic: for any given input and any desired output length the output is always the same.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Definition">Definition</h2></div>
<blockquote>
<p>A mask generation function takes an octet string of variable length and a desired output length as input, and outputs an octet string of the desired length. There may be restrictions on the length of the input and output octet strings, but such bounds are generally very large. Mask generation functions are deterministic; the octet string output is completely determined by the input octet string. The output of a mask generation function should be pseudorandom, that is, if the seed to the function is unknown, it should be infeasible to distinguish the output from a truly random string.<sup id="cite_ref-rsa_1-0" class="reference"><a href="#cite_note-rsa-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
</blockquote>
<div class="mw-heading mw-heading2"><h2 id="Applications">Applications</h2></div>
<p>Mask generation functions, as generalizations of hash functions, are useful wherever hash functions are. However, use of a MGF is desirable in cases where a fixed-size hash would be inadequate. Examples include generating <a href="Padding_(cryptography)" title="Padding (cryptography)">padding</a>, producing <a href="One-time_pad" title="One-time pad">one-time pads</a> or <a href="Keystream" title="Keystream">keystreams</a> in <a href="Symmetric-key_algorithm" title="Symmetric-key algorithm">symmetric-key encryption</a>, and yielding outputs for <a href="Pseudorandom_number_generator" title="Pseudorandom number generator">pseudorandom number generators</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Padding_schemes">Padding schemes</h3></div>
<p>Mask generation functions were first proposed as part of the specification for padding in the <a href="Optimal_asymmetric_encryption_padding" title="Optimal asymmetric encryption padding">RSA-OAEP</a> algorithm. The OAEP algorithm required a cryptographic hash function that could generate an output equal in size to a "data block" whose length was proportional to arbitrarily sized input message.<sup id="cite_ref-rsa_1-1" class="reference"><a href="#cite_note-rsa-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Random_number_generators">Random number generators</h3></div>
<p>NIST Special Publication 800-90A<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> defines a class of cryptographically secure random number generators, one of which is the "Hash DRBG", which uses a hash function with a counter to produce a requested sequence of random bits equal in size to the requested number of random bits.
</p>
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<p>Perhaps the most common and straightforward mechanism to build a MGF is to iteratively apply a hash function together with an incrementing counter value. The counter may be incremented indefinitely to yield new output blocks until a sufficient amount of output is collected. This is the approach used in MGF1.
</p>
<div class="mw-heading mw-heading3"><h3 id="MGF1">MGF1</h3></div>
<p>MGF1 is a mask generation function defined in the Public Key Cryptography Standard #1 published by RSA Laboratories:<sup id="cite_ref-rsa_1-2" class="reference"><a href="#cite_note-rsa-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<blockquote>
<div class="mw-heading mw-heading4"><h4 id="Options">Options</h4></div>
<p><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {Hash}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">H</mi>
<mi mathvariant="sans-serif">a</mi>
<mi mathvariant="sans-serif">s</mi>
<mi mathvariant="sans-serif">h</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {Hash}}}</annotation>
</semantics>
</math></span><img src="./4aa3706a65b2fe61a60ac537bc3e9a368c1ea506.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:4.857ex; height:2.176ex;" alt="{\displaystyle {\mathsf {Hash}}}" loading="lazy"></span>
</p>
<dl><dd>hash function (<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {hLen}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">h</mi>
<mi mathvariant="sans-serif">L</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">n</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {hLen}}}</annotation>
</semantics>
</math></span><img src="./d290e4a0484f8096d7e35c098a2ab0eee5f168d9.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:4.696ex; height:2.176ex;" alt="{\displaystyle {\mathsf {hLen}}}" loading="lazy"></span> denotes the length in octets of the hash function output)</dd></dl>
<div class="mw-heading mw-heading4"><h4 id="Input">Input</h4></div>
<p><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle Z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>Z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle Z}</annotation>
</semantics>
</math></span><img src="./1cc6b75e09a8aa3f04d8584b11db534f88fb56bd.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.68ex; height:2.176ex;" alt="{\displaystyle Z}" loading="lazy"></span>
</p>
<dl><dd>seed from which mask is generated, an octet string</dd></dl>
<p><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle l}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>l</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle l}</annotation>
</semantics>
</math></span><img src="./829091f745070b9eb97a80244129025440a1cfac.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:0.693ex; height:2.176ex;" alt="{\displaystyle l}" loading="lazy"></span>
</p>
<dl><dd>intended length in octets of the mask, at most <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{32}({\mathsf {hLen}})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mn>32</mn>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">h</mi>
<mi mathvariant="sans-serif">L</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">n</mi>
</mrow>
</mrow>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{32}({\mathsf {hLen}})}</annotation>
</semantics>
</math></span><img src="./440f273b98e02cafc11fd5c69425a136f2ec92fa.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:9.544ex; height:3.176ex;" alt="{\displaystyle 2^{32}({\mathsf {hLen}})}" loading="lazy"></span></dd></dl>
<div class="mw-heading mw-heading4"><h4 id="Output">Output</h4></div>
<p><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {mask}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">m</mi>
<mi mathvariant="sans-serif">a</mi>
<mi mathvariant="sans-serif">s</mi>
<mi mathvariant="sans-serif">k</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {mask}}}</annotation>
</semantics>
</math></span><img src="./9dd5562a4414f472754a8b08cb241699c3b2a148.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:4.991ex; height:2.176ex;" alt="{\displaystyle {\mathsf {mask}}}" loading="lazy"></span>
</p>
<dl><dd>mask, an octet string of length <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle l}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>l</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle l}</annotation>
</semantics>
</math></span><img src="./829091f745070b9eb97a80244129025440a1cfac.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:0.693ex; height:2.176ex;" alt="{\displaystyle l}" loading="lazy"></span>; or "mask too long"</dd></dl>
<div class="mw-heading mw-heading4"><h4 id="Steps">Steps</h4></div>
<div><ol style="list-style-type:decimal"><li>If <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle l>2^{32}({\mathsf {hLen}})}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>l</mi>
<mo>></mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mn>32</mn>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">h</mi>
<mi mathvariant="sans-serif">L</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">n</mi>
</mrow>
</mrow>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle l>2^{32}({\mathsf {hLen}})}</annotation>
</semantics>
</math></span><img src="./0eab502edf30e3db8fb6a20261e906c76a77d786.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:13.336ex; height:3.176ex;" alt="{\displaystyle l>2^{32}({\mathsf {hLen}})}" loading="lazy"></span>, output "mask too long" and stop.</li><li>Let <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T}</annotation>
</semantics>
</math></span><img src="./ec7200acd984a1d3a3d7dc455e262fbe54f7f6e0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.636ex; height:2.176ex;" alt="{\displaystyle T}" loading="lazy"></span> be the empty octet string.</li><li>For <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {counter}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">c</mi>
<mi mathvariant="sans-serif">o</mi>
<mi mathvariant="sans-serif">u</mi>
<mi mathvariant="sans-serif">n</mi>
<mi mathvariant="sans-serif">t</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">r</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {counter}}}</annotation>
</semantics>
</math></span><img src="./6226b76a6b5f1a03cfd498acfe2ec67ce80aa3b0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:7.266ex; height:1.843ex;" alt="{\displaystyle {\mathsf {counter}}}" loading="lazy"></span> from <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 0}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mn>0</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 0}</annotation>
</semantics>
</math></span><img src="./2aae8864a3c1fec9585261791a809ddec1489950.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.162ex; height:2.176ex;" alt="{\displaystyle 0}" loading="lazy"></span> to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \left\lceil {\tfrac {l}{\mathsf {hLen}}}\right\rceil -1}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow>
<mo>⌈</mo>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="false" scriptlevel="0">
<mfrac>
<mi>l</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">h</mi>
<mi mathvariant="sans-serif">L</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">n</mi>
</mrow>
</mfrac>
</mstyle>
</mrow>
<mo>⌉</mo>
</mrow>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \left\lceil {\tfrac {l}{\mathsf {hLen}}}\right\rceil -1}</annotation>
</semantics>
</math></span><img src="./bdfa92f705acca0e057376001625af28b019580e.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.838ex; width:10.615ex; height:4.843ex;" alt="{\displaystyle \left\lceil {\tfrac {l}{\mathsf {hLen}}}\right\rceil -1}" loading="lazy"></span>, do the following:
<div><ol style="list-style-type:lower-alpha"><li>Convert <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {counter}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">c</mi>
<mi mathvariant="sans-serif">o</mi>
<mi mathvariant="sans-serif">u</mi>
<mi mathvariant="sans-serif">n</mi>
<mi mathvariant="sans-serif">t</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">r</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {counter}}}</annotation>
</semantics>
</math></span><img src="./6226b76a6b5f1a03cfd498acfe2ec67ce80aa3b0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:7.266ex; height:1.843ex;" alt="{\displaystyle {\mathsf {counter}}}" loading="lazy"></span> to an octet string <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle C}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>C</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle C}</annotation>
</semantics>
</math></span><img src="./4fc55753007cd3c18576f7933f6f089196732029.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.766ex; height:2.176ex;" alt="{\displaystyle C}" loading="lazy"></span> of length <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 4}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mn>4</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 4}</annotation>
</semantics>
</math></span><img src="./295b4bf1de7cd3500e740e0f4f0635db22d87b42.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.162ex; height:2.176ex;" alt="{\displaystyle 4}" loading="lazy"></span> with the primitive <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\mathsf {I2OSP}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">I</mi>
<mn mathvariant="sans-serif">2</mn>
<mi mathvariant="sans-serif">O</mi>
<mi mathvariant="sans-serif">S</mi>
<mi mathvariant="sans-serif">P</mi>
</mrow>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\mathsf {I2OSP}}}</annotation>
</semantics>
</math></span><img src="./0278a7432ba361893775d07e561c5121dada47dc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:6.298ex; height:2.176ex;" alt="{\displaystyle {\mathsf {I2OSP}}}" loading="lazy"></span>:
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle C={\mathsf {I2OSP}}({\mathsf {counter}},4)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>C</mi>
<mo>=</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">I</mi>
<mn mathvariant="sans-serif">2</mn>
<mi mathvariant="sans-serif">O</mi>
<mi mathvariant="sans-serif">S</mi>
<mi mathvariant="sans-serif">P</mi>
</mrow>
</mrow>
<mo stretchy="false">(</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">c</mi>
<mi mathvariant="sans-serif">o</mi>
<mi mathvariant="sans-serif">u</mi>
<mi mathvariant="sans-serif">n</mi>
<mi mathvariant="sans-serif">t</mi>
<mi mathvariant="sans-serif">e</mi>
<mi mathvariant="sans-serif">r</mi>
</mrow>
</mrow>
<mo>,</mo>
<mn>4</mn>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle C={\mathsf {I2OSP}}({\mathsf {counter}},4)}</annotation>
</semantics>
</math></span><img src="./6498cc9255e2d43d990cdd43ee5ccf8e7649efe6.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:22.434ex; height:2.843ex;" alt="{\displaystyle C={\mathsf {I2OSP}}({\mathsf {counter}},4)}" loading="lazy"></span></dd></dl></li><li>Concatenate the hash of the seed <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle Z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>Z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle Z}</annotation>
</semantics>
</math></span><img src="./1cc6b75e09a8aa3f04d8584b11db534f88fb56bd.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.68ex; height:2.176ex;" alt="{\displaystyle Z}" loading="lazy"></span> and <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle C}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>C</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle C}</annotation>
</semantics>
</math></span><img src="./4fc55753007cd3c18576f7933f6f089196732029.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.766ex; height:2.176ex;" alt="{\displaystyle C}" loading="lazy"></span> to the octet string <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T}</annotation>
</semantics>
</math></span><img src="./ec7200acd984a1d3a3d7dc455e262fbe54f7f6e0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.636ex; height:2.176ex;" alt="{\displaystyle T}" loading="lazy"></span>:
<dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T=T\shortparallel {\mathsf {Hash}}(Z\shortparallel C)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
<mo>=</mo>
<mi>T</mi>
<mo class="MJX-variant">∥<!-- ∥ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="sans-serif">H</mi>
<mi mathvariant="sans-serif">a</mi>
<mi mathvariant="sans-serif">s</mi>
<mi mathvariant="sans-serif">h</mi>
</mrow>
</mrow>
<mo stretchy="false">(</mo>
<mi>Z</mi>
<mo class="MJX-variant">∥<!-- ∥ --></mo>
<mi>C</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T=T\shortparallel {\mathsf {Hash}}(Z\shortparallel C)}</annotation>
</semantics>
</math></span><img src="./b171674de40812d0caada781e054f50be380c745.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:20.873ex; height:2.843ex;" alt="{\displaystyle T=T\shortparallel {\mathsf {Hash}}(Z\shortparallel C)}" loading="lazy"></span></dd></li></ol></div></li><li>Output the leading <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle l}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>l</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle l}</annotation>
</semantics>
</math></span><img src="./829091f745070b9eb97a80244129025440a1cfac.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:0.693ex; height:2.176ex;" alt="{\displaystyle l}" loading="lazy"></span> octets of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T}</annotation>
</semantics>
</math></span><img src="./ec7200acd984a1d3a3d7dc455e262fbe54f7f6e0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.636ex; height:2.176ex;" alt="{\displaystyle T}" loading="lazy"></span> as the octet string mask.</li></ol></div>
</blockquote>
<div class="mw-heading mw-heading3"><h3 id="Example_code">Example code</h3></div>
<p>Below is Python code implementing MGF1:
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="kn">import</span><span class="w"> </span><span class="nn">hashlib</span>
<span class="k">def</span><span class="w"> </span><span class="nf">mgf1</span><span class="p">(</span><span class="n">seed</span><span class="p">:</span> <span class="nb">bytes</span><span class="p">,</span> <span class="n">length</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">hash_func</span><span class="o">=</span><span class="n">hashlib</span><span class="o">.</span><span class="n">sha1</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bytes</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Mask generation function."""</span>
<span class="n">hLen</span> <span class="o">=</span> <span class="n">hash_func</span><span class="p">()</span><span class="o">.</span><span class="n">digest_size</span>
<span class="c1"># https://www.ietf.org/rfc/rfc2437.txt</span>
<span class="c1"># 1. If l > 2^32(hLen), output "mask too long" and stop.</span>
<span class="k">if</span> <span class="n">length</span> <span class="o">></span> <span class="p">(</span><span class="n">hLen</span> <span class="o"><<</span> <span class="mi">32</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"mask too long"</span><span class="p">)</span>
<span class="c1"># 2. Let T be the empty octet string.</span>
<span class="n">T</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">""</span>
<span class="c1"># 3. For counter from 0 to \lceil{l / hLen}\rceil-1, do the following:</span>
<span class="c1"># Note: \lceil{l / hLen}\rceil-1 is the number of iterations needed,</span>
<span class="c1"># but it's easier to check if we have reached the desired length.</span>
<span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">T</span><span class="p">)</span> <span class="o"><</span> <span class="n">length</span><span class="p">:</span>
<span class="c1"># a. Convert counter to an octet string C of length 4 with the primitive I2OSP: C = I2OSP (counter, 4)</span>
<span class="n">C</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">to_bytes</span><span class="p">(</span><span class="n">counter</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="s2">"big"</span><span class="p">)</span>
<span class="c1"># b. Concatenate the hash of the seed Z and C to the octet string T: T = T || Hash (Z || C)</span>
<span class="n">T</span> <span class="o">+=</span> <span class="n">hash_func</span><span class="p">(</span><span class="n">seed</span> <span class="o">+</span> <span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
<span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="c1"># 4. Output the leading l octets of T as the octet string mask.</span>
<span class="k">return</span> <span class="n">T</span><span class="p">[:</span><span class="n">length</span><span class="p">]</span>
</pre></div>
<p>Example outputs of MGF1:
</p>
<div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span class="go">Python 3.10.4 (main, Apr 16 2022, 16:28:41) [GCC 8.3.0] on linux</span>
<span class="go">Type "help", "copyright", "credits" or "license" for more information.</span>
<span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">mgf1</span><span class="w"> </span><span class="kn">import</span> <span class="n">mgf1</span>
<span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">hashlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">sha256</span>
<span class="gp">>>> </span><span class="n">mgf1</span><span class="p">(</span><span class="sa">b</span><span class="s2">"foo"</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">'1ac907'</span>
<span class="gp">>>> </span><span class="n">mgf1</span><span class="p">(</span><span class="sa">b</span><span class="s2">"foo"</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">'1ac9075cd4'</span>
<span class="gp">>>> </span><span class="n">mgf1</span><span class="p">(</span><span class="sa">b</span><span class="s2">"bar"</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">'bc0c655e01'</span>
<span class="gp">>>> </span><span class="n">mgf1</span><span class="p">(</span><span class="sa">b</span><span class="s2">"bar"</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">'bc0c655e016bc2931d85a2e675181adcef7f581f76df2739da74faac41627be2f7f415c89e983fd0ce80ced9878641cb4876'</span>
<span class="gp">>>> </span><span class="n">mgf1</span><span class="p">(</span><span class="sa">b</span><span class="s2">"bar"</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="n">sha256</span><span class="p">)</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">'382576a7841021cc28fc4c0948753fb8312090cea942ea4c4e735d10dc724b155f9f6069f289d61daca0cb814502ef04eae1'</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-rsa-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-rsa_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-rsa_1-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-rsa_1-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFRSA_Laboratories" class="citation web cs1">RSA Laboratories. <a rel="nofollow" class="external text" href="https://www.ietf.org/rfc/rfc2437.txt">"RFC 2437 PKCS #1"</a>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFNational_Institute_of_Standards_and_Technology2012" class="citation journal cs1">National Institute of Standards and Technology (2012). <a rel="nofollow" class="external text" href="http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf">"Recommendation for Random Number Generation Using Deterministic Random Bit Generators"</a> <span class="cs1-format">(PDF)</span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.6028%2FNIST.SP.800-90A">10.6028/NIST.SP.800-90A</a>.</cite> <span class="cs1-visible-error citation-comment"><code class="cs1-code">{{cite journal}}</code>: </span><span class="cs1-visible-error citation-comment">Cite journal requires <code class="cs1-code">|journal=</code> (help)</span></span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2024-04-08" href="https://en.wikipedia.org/wiki/?title=Mask_generation_function&oldid=1217939385">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>